package code.structure.module.friends
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import code.main.AppData;
	
	/**
	 * 读取FaceBook好友
	 * @author CYang
	 */
	public class ReadFaceBookFriends
	{
		private static var _fb:ReadFaceBookFriends;
		
		public function ReadFaceBookFriends(single:Single)
		{
		
		}
		
		public static function getInstance():ReadFaceBookFriends
		{
			return _fb ||= new ReadFaceBookFriends(new Single());
		}
		/**
		 * 读取FaceBook好友的信息
		 * @param	callBack 正确回调
		 * @param	errBack	错误回调
		 */
		public function readFriends(callBack:Function = null, errBack:Function = null):void
		{
			var str:String = "https://graph.facebook.com/me/friends?fields=id,name,first_name,gender,installed,picture&limit=5000&access_token=" + AppData.accessToken;
//			var str:String = "https://graph.facebook.com/fql?q=select%20uid%2Cis_app_user%2Cname%2Cfirst_name%2Csex%2C%20pic_square%20FROM%20user%20WHERE%20uid%20in%20(Select%20uid2%20from%20friend%20where%20uid1%3Dme())%0A&format=json&access_token=" + AppData.accessToken;
			load(str, callBack, errBack);
		}
		private function load(str:String, callBack:Function = null, errBack:Function = null):void
		{
			var request:URLRequest = new URLRequest(str);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			//loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, loadComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorEventHandler);
			loader.load(request);
			
			function loadComplete(e:Event):void
			{
				var _obj:Object = JSON.parse(e.target.data);
				if (callBack != null) callBack(_obj.data);
				clearLoad();
			}
			function securityErrorEventHandler(e:SecurityErrorEvent):void
			{
				trace("securityErrorEventHandler:===============>>>", e);
				if (errBack != null) errBack();
				clearLoad();
			}
			function ioError(e:IOErrorEvent):void
			{
				trace("IOErrorEvent:===============>>>",e);
				if (errBack != null) errBack();
				clearLoad();
			}
			function clearLoad():void
			{
				loader.removeEventListener(Event.COMPLETE, loadComplete);
				loader.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
				loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorEventHandler);
				loader = null;
			}
		}
		/**
		 * facebook与当地货币的转换信息
		 * @param	callBack
		 */
		public function readCurrency(callBack:Function=null,errorBack:Function=null):void
		{
			var str:String = "https://graph.facebook.com/me/?fields=currency&access_token=" + AppData.accessToken;
			load(str, currencycallback, errorBack);
			function currencycallback(obj:Object):void
			{
				if (callBack != null)
					callBack(obj.currency);
			}
		}
	}

}

class Single
{
}